<?php
defined( 'BASEPATH' ) or exit( 'No direct script access allowed' );
class Setting_handle extends MY_Controller
{
    function get()
    {
        $this->load->model( 'setting_model' );
        if ($this->setting_model->load( 0 ))
        {
            $this->rs->ok( $this->setting_model->get() );
        }
        $this->rs->err();
    }
    function get_super()
    {
        $this->load->model( 'setting_model' );
        if ($this->setting_model->load())
        {
            $this->rs->ok( $this->setting_model->get() );
        }
        $this->rs->err();
    }
    private function _modify($gid = null)
    {
        $this->load->model( 'setting_model' );
        if (! $this->setting_model->load( $gid ))
        {
            $this->rs->err();
        }
        $post = $this->input->post();
        foreach ( $this->setting_model->get() as $k => $v )
        {
            if (array_key_exists( $k, $post ))
            {
                $modify_data = array ();
                if (is_bool( $v ))
                {
                    $modify_data['value'] = $post[$k] ? '1' : '0';
                }
                elseif (is_int( $v ))
                {
                    $modify_data['value'] = ( int ) $post[$k];
                }
                else
                {
                    $modify_data['value'] = substr_utf8( $post[$k], 5000 );
                }
                if (false === $this->setting_model->update( $modify_data, $k, $id_field = '`key`' ))
                {
                    $this->rs->err( "更新“$k”时出现错误！" );
                }
            }
        }
        $this->rs->ok();
    }
    function modify()
    {
        $this->_modify( 0 );
    }
    function modify_super()
    {
        $this->_modify();
    }
}